package com.demo.core.service.base.impl;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import com.demo.core.dao.SiteFunctionDao;
import com.demo.core.model.Function;
import com.demo.core.model.SiteFunction;
import com.demo.core.service.base.SiteFunctionService;

public class SiteFunctionServiceImpl implements SiteFunctionService {

	/**
	 * 站点功能Dao
	 */
	private SiteFunctionDao siteFunctionDao;
	
	@Override
	public void deleteAll(Long siteId) {
		Map<String, Object> params = new HashMap<String, Object>(3);
		params.put("siteId", siteId);
		siteFunctionDao.delete(params);
	}
	
	@Override
	@Transactional
	public void saveAll(Long siteId, boolean enabled, List<Function> functionList) {
		if (functionList != null) {
			for (Function f : functionList) {
				SiteFunction sf = new SiteFunction();
				sf.setFunctionId(f.getId());
				sf.setSiteId(siteId);
				sf.setEnabled(enabled);
				//可读写删除
				sf.setStatus("RWD");
				siteFunctionDao.insert(sf);
				// 释放空间,以便垃圾回收
				sf = null;
			}
		}
	}

	@Override
	public void update(Long siteId, Long functionId, String status,	boolean enabled) {
		// 更新模型
		SiteFunction sf = new SiteFunction();
		sf.setStatus(status);
		sf.setEnabled(enabled);
		// 更新条件
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("siteId", siteId);
		params.put("functionId", functionId);
		
		siteFunctionDao.update(sf, params);
	}
	
	@Override
	public void updateAll(Long siteId, String status, boolean enabled) {
		SiteFunction sf = new SiteFunction();
		sf.setSiteId(siteId);
		sf.setStatus(status);
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("siteId", siteId);
		siteFunctionDao.update(sf, params);
	}
	
	@Override
	public Long save(SiteFunction t) {
		return siteFunctionDao.insert(t);
	}

	@Override
	public boolean exists(Long id) {
		return siteFunctionDao.exists(id);
	}

	@Override
	public void delete(Long id) {
		siteFunctionDao.delete(id);
	}

	@Override
	public void update(Long id, String status, boolean enabled) {
		Map<String, Object> params = new HashMap<String, Object>(2);
		params.put("id", id);
		SiteFunction siteFunction = new SiteFunction();
		siteFunction.setStatus(status);
		siteFunction.setEnabled(enabled);
		siteFunctionDao.update(siteFunction, params);
	}

	@Override
	public SiteFunction find(Long id) {
		return siteFunctionDao.select(id);
	}

	@Override
	public List<SiteFunction> findAll(Long siteId) {
		Map<String, Object> params = new HashMap<String, Object>(2);
		params.put("siteId", siteId);
		return siteFunctionDao.select(params);
	}

	@Override
	public List<SiteFunction> query(Long siteId) {
		Map<String, Object> params = new HashMap<String, Object>(2);
		params.put("siteId", siteId);
		return siteFunctionDao.queryFunctionUnion(params);
	}

	@Override
	public List<SiteFunction> query(Long siteId, String i18n) {
		Map<String, Object> params = new HashMap<String, Object>(2);
		params.put("i18n", i18n);
		params.put("siteId", siteId);
		return siteFunctionDao.queryFunctionUnion(params);
	}
	
	@Override
	public List<SiteFunction> query(Long siteId, Long functionId) {
		Map<String, Object> params = new HashMap<String, Object>(2);
		params.put("functionId", functionId);
		params.put("siteId", siteId);
		return siteFunctionDao.queryFunctionUnion(params);
	}
	
	@Override
	public SiteFunction queryFunctionTree(Long id, Long siteId, Long siteGroupId, int... depths) {
		Map<String, Object> params = new HashMap<String, Object>(2);
		params.put("id", id);
		params.put("siteId", siteId);
		params.put("siteGroupId", siteGroupId);
		params.put("depthList", CollectionUtils.arrayToList(depths));
		return siteFunctionDao.queryFunctionTree(params);
	}

	@Override
	public SiteFunction queryFunctionTree(Long id, Long siteId, int... depths) {
		return queryFunctionTree(id, siteId, null, depths);
	}
	
	@Override
	public SiteFunction queryFunctionTreeRoot(Long siteId, int... depths) {
		return queryFunctionTree(null, siteId, null, depths);
	}
	
	@Override
	public SiteFunction querySiteFunctionTree(Long id, Long siteId, Long siteGroupId, int... depths) {
		Map<String, Object> params = new HashMap<String, Object>(7);
		params.put("id", id);
		params.put("siteId", siteId);
		params.put("siteGroupId", siteGroupId);
		params.put("depthList", CollectionUtils.arrayToList(depths));
		return siteFunctionDao.querySiteFunctionTree(params);
	}
	
	@Override
	public SiteFunction querySiteFunctionTreeRoot(Long siteId, int... depths) {
		// 节点ID为null
		return queryFunctionTree(null, siteId, null, depths);
	}

	@Override
	public SiteFunction querySiteFunctionTree(Long id, Long siteId, int... depths) {
		return querySiteFunctionTree(id, siteId, null, depths);
	}
	
	@Override
	public Map<Long, SiteFunction> querySiteFunctionMap(Long siteId, Boolean enabled) {
		Map<String, Object> params = new HashMap<String, Object>(3);
		params.put("siteId", siteId);
		params.put("sfEnabled", enabled);
		return siteFunctionDao.querySiteFunctionMap(params);
	}
	
	@Override
	public List<SiteFunction> querySiteFunctionAndRoleList(Long siteId, String type) {
		Map<String, Object> params = new HashMap<String, Object>(3);
		params.put("siteId", siteId);
		params.put("functionType", type);
		return siteFunctionDao.querySiteFunctionAndRoleList(params);
	}
	
	@Override
	public Map<Long, SiteFunction> querySiteFunctionAndRoleMap(Long siteId,	String type) {
		Map<String, Object> params = new HashMap<String, Object>(3);
		params.put("siteId", siteId);
		params.put("functionType", type);
		List<SiteFunction> siteFunctionList = siteFunctionDao.querySiteFunctionAndRoleList(params);
		Map<Long, SiteFunction> siteFunctionMap = new LinkedHashMap<Long, SiteFunction>();
		for (SiteFunction sf : siteFunctionList) {
			com.demo.core.model.Function f = sf.getFunction();
			if (f != null) {
				siteFunctionMap.put(f.getId(), sf);	
			}
		}
		return siteFunctionMap;
	}
	
	public void setSiteFunctionDao(SiteFunctionDao siteFunctionDao) {
		this.siteFunctionDao = siteFunctionDao;
	}
}
